﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lilium
{
	public static class ServiceProviderExtensions
	{
		public static object GetService(this Lilium.IServiceProvider serviceProvider, Type serviceType, bool required)
		{
			if (serviceProvider == null) throw new ArgumentNullException("serviceProvider");

			var service = serviceProvider.GetService(serviceType);
			if (required && (service == null))
				throw new InvalidOperationException(string.Format("Missing required service of type {0}", serviceType));

			return service;
		}

		public static TServiceType GetService<TServiceType>(this Lilium.IServiceProvider serviceProvider, Type serviceType, bool required)
		{
			if (serviceProvider == null) throw new ArgumentNullException("serviceProvider");

			var service = serviceProvider.GetService(serviceType);
			if (required && (service == null))
				throw new InvalidOperationException(string.Format("Missing required service of type {0}", serviceType));

			return (TServiceType)service;
		}

		public static TServiceType GetService<TServiceType>(this Lilium.IServiceProvider serviceProvider, bool required)
		{
			if (serviceProvider == null) throw new ArgumentNullException("serviceProvider");

			var service = serviceProvider.GetService<TServiceType>();
			if (required && (service == null))
				throw new InvalidOperationException(string.Format("Missing required service of type {0}", typeof(TServiceType)));

			return service;
		}
	}
}
